package com.gem.back.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gem.back.entity.AoaNoticeList;
import com.gem.back.entity.AoaUser;
import com.gem.back.mapper.AoaNoticeListMapper;
import com.gem.back.service.IAoaNoticeListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import com.gem.back.service.IAoaUserService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhhy
 * @since 2020-02-22
 */
@Service
public class AoaNoticeListServiceImpl extends ServiceImpl<AoaNoticeListMapper, AoaNoticeList> implements IAoaNoticeListService {
    @Autowired
    private AoaNoticeListMapper aoaNoticeListMapper;
    @Autowired
    private IAoaUserService aoaUserService;

    @Override
    public List<AoaNoticeList> findNoticeList(Integer currentPage, Integer pageCount, String title) {
        if (title == null){
            title = "";
        }
        return aoaNoticeListMapper.findNoticeList((currentPage - 1) * pageCount,pageCount,title);
    }

    @Override
    public Integer getTotalPage(Integer pageCount, String title) {
        if (title == null){
            title = "";
        }
        Integer total = aoaNoticeListMapper.getTotal(title);
        return (total - 1) / pageCount + 1;
    }

    @Override
    public AoaNoticeList findNoticeById(Integer id) {
        return aoaNoticeListMapper.findNoticeById(id);
    }

    @Override
    public void saveNotice(AoaNoticeList aoaNoticeList,HttpServletRequest request) {
        String top = request.getParameter("Top");
        if (top != null){
            aoaNoticeList.setIsTop(1);
        }else {
            aoaNoticeList.setIsTop(0);
        }
        if (aoaNoticeList.getNoticeId() != null){
            aoaNoticeListMapper.updateById(aoaNoticeList);
        }else {
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
            AoaUser aoaUser = aoaUserService.findUserByName(userDetails.getUsername());
            aoaNoticeList.setUserId(aoaUser.getUserId());
            aoaNoticeList.setNoticeTime(new Date());
            aoaNoticeListMapper.insert(aoaNoticeList);
        }
    }

    @Override
    public List<AoaNoticeList> findMyNoticeList(Integer currentPage, Integer pageCount, String title, Integer id) {
        if (title == null){
            title = "";
        }
        return aoaNoticeListMapper.findMyNoticeList((currentPage - 1) * pageCount,pageCount,title,id);
    }

    @Override
    public Integer getMyTotalPage(Integer pageCount, String title, Integer id) {
        if (title == null){
            title = "";
        }
        Integer myTotal = aoaNoticeListMapper.getMyTotal(title,id);
        return (myTotal - 1) / pageCount + 1;
    }

    @Override
    public List<AoaNoticeList> findMyNoticeById(Integer userId) {
        return aoaNoticeListMapper.findMyNoticeById(userId);
    }

    @Override
    public Integer getMyTotal(String title, Integer id) {
        return aoaNoticeListMapper.getMyTotal(title,id);
    }
}
